﻿'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:
'******************************************************
'******************************************************
Imports System.Text
Imports System.ComponentModel
Namespace GGE
    ''' <summary>
    ''' This is the base Collection Class
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <remarks></remarks>
    Public Class BaseCollection(Of T)
        Inherits ObjectModel.Collection(Of T)
        Implements IObjectName
#Region "Fields"
        Private _Parent As Object = Nothing
        Private _IsDirty As Boolean = False
#End Region
#Region "Properties"
        <Browsable(False), CategoryAttribute("Misc"), DescriptionAttribute("Is this item or its childern dirty")> _
        Public Property IsDirty() As Boolean
            Get
                Return _IsDirty
            End Get
            Set(ByVal value As Boolean)
                _IsDirty = value
            End Set
        End Property
        <Browsable(False), CategoryAttribute("Misc"), DescriptionAttribute("Pointter to its parent")> _
        Protected ReadOnly Property Parent() As Object Implements IObjectName.Parent
            Get
                Return _Parent
            End Get
        End Property
        <Browsable(False)> _
        Public Overridable ReadOnly Property ObjectName() As String Implements IObjectName.ObjectName
            Get
                Return ""
            End Get
        End Property

#End Region
#Region "Methods"
        Public Sub SetDirty()
            If Me.Parent IsNot Nothing Then Me.Parent.SetDirty()
        End Sub
        Public Overridable Sub SetParent(ByVal ParentObject As Object) Implements IObjectName.SetParent
            _Parent = ParentObject
        End Sub
        ''' <summary>
        ''' Save the Project
        ''' </summary>
        ''' <param name="filename">File name and path</param>
        ''' <remarks></remarks>
        Public Sub Save(ByVal filename As String)
            Dim mlSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(T))
            Dim writer As IO.StringWriter = New IO.StringWriter()
            mlSerializer.Serialize(writer, Me)
            My.Computer.FileSystem.WriteAllText(filename, writer.ToString, False, Global.System.Text.Encoding.Default)
        End Sub
        ''' <summary>
        ''' Build the xml String
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overridable Function BuildXml() As String
            Return ""
        End Function
        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As T)
            MyBase.InsertItem(index, item)
            If TypeOf item Is IObjectName Then
                Dim TItem As IObjectName = CType(item, IObjectName)
                TItem.SetParent(Me)
            End If
        End Sub
#End Region
    End Class

    ''' <summary>
    ''' This is the base Item Class
    ''' </summary>
    ''' <remarks></remarks>
    Public Class BaseItem
        Implements IObjectName
#Region "Fields"
        Private _Parent As Object = Nothing
        Private _IsDirty As Boolean = False
#End Region
#Region "Properties"
        <Browsable(False), CategoryAttribute("Misc"), DescriptionAttribute("Is this item or its childern dirty")> _
        Public Property IsDirty() As Boolean
            Get
                Return _IsDirty
            End Get
            Set(ByVal value As Boolean)
                _IsDirty = value
            End Set
        End Property
        <Browsable(False), CategoryAttribute("Misc"), DescriptionAttribute("Pointter to its parent")> _
        Protected ReadOnly Property Parent() As Object Implements IObjectName.Parent
            Get
                Return _Parent
            End Get
        End Property
        <Browsable(False)> _
        Public Overridable ReadOnly Property ObjectName() As String Implements IObjectName.ObjectName
            Get
                Return ""
            End Get
        End Property

#End Region
#Region "Methods"
        Public Sub SetDirty()
            If Me.Parent IsNot Nothing Then Me.Parent.SetDirty()
        End Sub
        Public Overridable Sub SetParent(ByVal ParentObject As Object) Implements IObjectName.SetParent
            _Parent = ParentObject
        End Sub
        ''' <summary>
        ''' Save the Project
        ''' </summary>
        ''' <param name="filename">File name and path</param>
        ''' <remarks></remarks>
        Public Sub Save(ByVal filename As String)
            Dim mlSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(BaseItem))
            Dim writer As IO.StringWriter = New IO.StringWriter()
            mlSerializer.Serialize(writer, Me)
            My.Computer.FileSystem.WriteAllText(filename, writer.ToString, False, Global.System.Text.Encoding.Default)
        End Sub
        ''' <summary>
        ''' Build the xml String
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overridable Function BuildXml() As String
            Return ""
        End Function
#End Region
    End Class



End Namespace